<template>
  <div class="container">
    <div class="search-box">
      <!-- 头部 搜索 -->
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="inputValue"
      ></el-input>
      <el-button @click="cleanInput">清除</el-button>
      <el-button type="primary" @click="searchData">搜索</el-button>
    </div>
    <!-- 表格 -->
    <el-table border style="width: 100%" :data="list">
      <el-table-column
        type="index"
        label="任务编号"
        width="80"
        :index="indexMethod"
      >
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="editData(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="deleteData(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pageContainer">
      <el-pagination
        @current-change="pageChange"
        :current-page="params._page"
        :page-size="changSize ? 100 : params._limit"
        layout="total,prev, pager, next"
        :total="total"
      >
      </el-pagination>
      <el-button size="small" plain @click="addData" :disabled="changSize"
        >+ 新增</el-button
      >
    </div>
    <!-- 对话框 -->
    <add-dialog
      :title="dialogTitle"
      :dialog-visible="dialogVisible"
      :data-id="dataId"
      @close="closeDialog"
      @add-success="getList"
    >
    </add-dialog>
  </div>
</template>

<script>
import {
  fecthDataList,
  fecthDelData,
  fecthSearchData,
} from "@/api/data-api.js";
import AddDialog from "@/componets/AddDialog.vue";
export default {
  data() {
    return {
      list: [],
      total: null,
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      dialogTitle: "",
      dialogVisible: false,
      dataId: "",
      inputValue: "",
      changSize: false,
    };
  },
  methods: {
    // 获取列表数据
    async getList() {
      const res = await fecthDataList(this.params);
      // console.log(res.headers['x-total-count'])
      this.list = res.data;
      this.total = parseInt(res.headers["x-total-count"]);
    },
    // 页码改变
    pageChange(page) {
      this.params._page = page;
      this.getList();
    },
    // 自增序号
    indexMethod(index) {
      return (this.params._page - 1) * this.params._limit + index + 1;
    },
    // 新增数据
    addData() {
      this.dialogVisible = true;
      this.dialogTitle = "新增数据";
    },
    //关闭对话框
    closeDialog() {
      this.dialogVisible = false;
    },
    // 编辑数据
    editData(id) {
      this.dialogTitle = "编辑数据";
      this.dataId = id;
      this.dialogVisible = true;
    },
    // 删除数据
    async deleteData(id) {
      await fecthDelData(id);
      this.$message.success("删除成功");
      this.getList();
    },
    // 搜索
    async searchData() {
      if (this.inputValue.trim() === "")
        return this.$message.warning("搜索内容不可为空");
      // decodeURI 对字符串解码  汉字会出现乱码，使用decodeURI解码
      const value = decodeURI(this.inputValue);
      const { data } = await fecthSearchData(value);
      this.list = data;
      this.total = data.length;
      // 用来判断是否改变当前页面数据显示条数及新增按钮是否可用
      this.changSize = true;
      // 保证任务编号顺序
      this.params._page = 1;
    },
    // 清空input内容
    cleanInput() {
      console.log(111);
      this.inputValue = "";
      this.changSize = false;
      this.getList();
    },
  },
  mounted() {
    this.getList();
  },
  components: {
    AddDialog,
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
